package com.java.xiaoBao;

public class MiGongTest2 {

	public static void main(String[] args) {

		
		 int[][] map =
	            {
	                    //0 1 2 3 4 5 6 7 8 9 10                19
	                    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1},//0
	                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//1
	                    {1,1,1,0,1,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1},//2
	                    {1,1,1,0,1,1,0,0,0,0,0,0,1,1,1,0,0,1,0,1},//3
	                    {1,1,1,0,1,1,1,1,1,1,1,0,0,0,1,1,0,0,0,1},//4
	                    {1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1},//5
	                    {1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,1,1,1,1},//6
	                    {1,1,0,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1,1,1},//7
	                    {1,1,0,1,1,0,1,1,0,0,0,0,1,1,1,0,0,0,0,1},//8
	                    {1,0,0,1,1,0,1,0,1,1,0,1,1,0,1,1,0,1,1,1},//9
	                    {1,1,0,1,1,0,1,0,0,0,0,0,0,0,1,0,0,0,0,1},//10
	                    {1,0,0,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,1},//11
	                    {1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,0,1,1,1,1},//12
	                    {1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1},//13
	                    {1,0,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1},//14
	                    {1,1,0,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0},//15
	                    {1,1,0,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1},//16
	                    {1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},//17
	                    {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},//18
	                    {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1} //19
	            };


        System.out.println("========原来的迷宫图============");
        for (int i=0 ; i<20 ; i++){
            for (int j = 0; j < 20; j++) {
                System.out.print(map[i][j]+" ");
            }
            System.out.println();
        }


        setWay2(map , 0 ,18 );

        System.out.println("==========迷宫的解题=============");
        for (int i=0 ; i<20 ; i++){
            for (int j = 0; j < 20; j++) {
                System.out.print(map[i][j]+" ");
            }
            System.out.println();
        }

        
        
	}


    /**
     * 修改找路的策略（右-》下-》左-》上）
     */
    public static boolean setWay2(int[][] map , int i , int j){
        if (map[15][19] == 2){//说明通过找到
            return true;
        }else {
            if (map[i][j] == 0){//如果该点没有走过，就可以按定义的策略玩了
                //右-》下-》左-》上
                map[i][j] = 2;//先假定该点是可以走通的
                if (setWay2(map , i , j+1)){//向右走
                    return true;
                }else if (setWay2(map , i+1 , j)){ //else向下走
                    return true;
                }else if (setWay2(map , i , j-1)){ //else向左走
                    return true;
                }else if (setWay2(map , i-1 , j)){ //else向上走
                    return true;
                }else { //3表示该位置已经走过，但是走不通
                    //说明该点走不同，是死路
                    map[i][j] = 0;
                    return false;
                }
            }else { //如果map[i][j]不等于0，可能是 1；2；3
                //直接返回false
                return false;
            }
        }

    }
	
}
